package com.lsm.sm.service.impl;

import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.lsm.sm.dao.BaseDao;
//import com.lsm.sm.entity.ProxyUser;
import com.lsm.sm.entity.User;
import com.lsm.sm.service.UserService;
import com.lsm.sm.util.Const;
import com.lsm.sm.util.CookieUtil;
import com.lsm.sm.util.MD5Util;
import com.lsm.sm.util.WebUtil;
//import com.opensymphony.xwork2.ActionContext;

@Scope("prototype")
@Service("userService")
//@Scope("session")
public class UserServiceImp implements UserService{
	
	@Resource
	private BaseDao baseDao;
	/*
	 * 若用户名不存在，则不允许注册
	 */
	public void register( User user ) throws Exception{
		
		if( user==null || StringUtils.isEmpty(user.getProxyId()) || !catalogID(user) ){
			throw new Exception( "账号不正确有木有…");
		}
		String pwd = user.getPassword();
		if( pwd.length()<6 || pwd.length()>16 ){
			throw new Exception( "亲，你的密码不符合格式…");
		}
		
		user.setNickname( null );
		user.setPassword(null);
		
		if( baseDao.get( user ) !=null ){
			throw new Exception( "呵呵…账号被人占用！" );
		}
		user.setPassword( MD5Util.encode( pwd ) );
		baseDao.save( user );
	}
	
	private boolean catalogID( User user ){
		String ID = user.getProxyId();
		if( ID==null || ID.length()<6 ){
			return false;
		}
		
		String email = "^['_A-Za-z0-9-]+(\\.['_A-Za-z0-9-]+)*@([A-Za-z0-9-])+(\\.[A-Za-z0-9-]+)*((\\.[A-Za-z0-9]{2,})|(\\.[A-Za-z0-9]{2,}\\.[A-Za-z0-9]{2,}))$";
		/*String number = "^((1[0-2])|9)((?!0{3})\\d{3})((?!0{2})\\d{2}){2}$";*/
		/*String qq = "^(?!0{1})\\d{6,10}$";*/
		String phone = "^1[3|5|8][0-9]{9}$";
		
		if( ID.matches( email ) ){
			user.setEmail( ID );
		}else if( ID.matches(phone) ){
			user.setPhone( phone );
		}else{
			return false;
		}
		return true;
	}
	
	private void initProxyName( User user ){
		/*
		 * email -->
		 * phone -->
		 * nickname -->
		 * name -->
		 * 
		 * 默认将其 邮箱置为 名称
		 * 	若不行 若为空再置 手机 为其名称
		 */
		String proxyname = user.getEmail();
		
		if( StringUtils.isBlank( proxyname ) ){
			if( !StringUtils.isBlank( user.getPhone() ) ){
				proxyname = user.getPhone();
			}
		}
		
		//
		if( StringUtils.isBlank( user.getName() ) ){
			if( !StringUtils.isBlank( user.getPhone()) ){
				proxyname = user.getNickname();
			}
		}else{
			proxyname=user.getName();
		}
		
		user.setProxyId( proxyname );
	}
	
	public User login( User user ) throws Exception {
		if( user==null || StringUtils.isEmpty(user.getProxyId()) || !catalogID(user) ){
			throw new Exception( "亲，账号难以理解嘞…");
		}
		
		String idstr = user.getProxyId();
		String pwd = user.getPassword().trim();
		if( pwd!=null && pwd.matches( "^[A-Z0-9]{32}$") ){
			user.setPassword( pwd );
		}else{
			user.setPassword( MD5Util.encode(pwd) );
		}
		
		user.setNickname( null );
		User u2 = baseDao.get( user );
		if( u2 == null ){
			throw new Exception( "用户名或密码错误");
		}
		
		initProxyName( u2 );		//为 该 用户决策 其 网页显示名
		WebUtil.addInSession( Const.USER, u2 );
		CookieUtil.addCookie( Const.COOKIE_USER, idstr + ":" + u2.getPassword() );
		return u2;
	}
	
	public User checkExist( User user )throws Exception{
		if( user==null || StringUtils.isEmpty(user.getProxyId()) || !catalogID(user) ){
			throw new Exception( "亲，账号难以理解嘞…");
		}
		User u2 = baseDao.get( user );
		return u2;
	}
	
	/*private void addPUInSession( User user ){
		ProxyUser pu = new ProxyUser();
		pu.constructedFromUser( user );
		Map<String, Object> session = ActionContext.getContext().getSession();
		session.put( Const.USER, pu );
	}*/
	
	/*private void addUserInSession( User user ){
		Map<String, Object> session = ActionContext.getContext().getSession();
		session.put( Const.USER, pu );
		WebUtil.addInSession( Const.USER, user );
	}*/
	
	/*private ProxyUser getFromSession() throws Exception{
		Map<String, Object> session = WebUtil.getSession();
		ProxyUser pu = (ProxyUser)session.get( "user" );
		if( pu == null ){
			throw new Exception( "please login first!" );
		}
		return pu;
	}*/
	
	public User getUser()throws Exception{
		//ProxyUser pu = getFromSession();
		User pu = WebUtil.getUser();
		if( pu == null ){
			throw new Exception( "please login first!" );
		}
		User u2 =baseDao.get( User.class, pu.getId() );
		if( u2 == null ){
			throw new Exception( "用户不存在" );
		}
		return u2;
	}
	
	public void logout(){
		Map<String, Object> session = WebUtil.getSession();
		User user = (User)session.get( "user" );
		
		if( user != null ){
			session.remove( "user" );
		}
	}
	
	/*
	 * 1. fetch the user from the database;
	 * 2. compare the persistent user to the transient one
	 * 3. update the available field
	 */
	public void update( User user ) throws Exception {
		User pstUser = baseDao.get( User.class, user.getId() );
		if( pstUser == null ){
			throw new Exception( "The user doesn't exist!" );
		}
		baseDao.update( user );
	}
}
